<?php

require_once('Interface.php');
require_once(ISLANDS_PATH . '/Smarty/libs/Smarty.class.php');

class CodeIslands_Api_Service_Template_Engine_Smarty implements CodeIslands_Api_Service_Template_Engine_Interface
{
	private $_smarty = null;
	private $_template_file_service = null;
	
	public function __construct($filters=null, $template_file_service)
	{
		$this->_template_file_service = $template_file_service;
		
		$this->_smarty = new Smarty();
		$this->_smarty->compile_dir = ISLANDS_PATH . '/templates_c';
		$this->_smarty->plugins_dir = ISLANDS_PATH . '/sysplugins';
		$this->_smarty->autoload_filters = $filters;
	}
	
	public function fetch($template_data, $template_name='ui')
	{
		$full_template_path = $this->_template_file_service->getFullTemplatePath(
			ISLAND_PLUGINS_PATH . '/' . $template_data['name'], 
			isset($template_data['media']) ? $template_data['media'] : null,
			isset($template_data['language']) ? $template_data['language'] : null, 
			isset($template_data['locale']) ? $template_data['locale'] : null,
			$template_name);
		
		$this->_smarty->assign('data', $template_data);

		return $this->_smarty->fetch($full_template_path);
	}
}